<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



class Upload extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	}

	function index()
	{
		$this->load->view('upload_form', array('error' => ' ' ));
	}

	function do_upload()
	{
		$config['upload_path'] = './assets/uploads/';
		$config['allowed_types'] = 'csv';
		$config['file_name'] = 'tempcsv';
		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			$this->load->view('upload_form', $error);
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());
			$this->load->view('upload_success', $data);
			$file = fopen("./assets/uploads/tempcsv.csv","r");
			$data = array();
			$header = fgetcsv($file);
			while (($row = fgetcsv($file)) !== false) {
		 	   $data[] = array_combine($header, $row);
		 	}
		 	$this->db->insert_batch('test', $data);
		 	$this->load->helper("file");
			
		 }
			  fclose($file);
			  delete_files("./assets/uploads/");
		
	}
}

/* End of file index.php */
/* Location: ./application/controllers/upload.php */